import axios from "axios"
const service = axios.create({
    baseURL:"/dataApi",// url = base url + request url
    timeout:1000000
})

// 请求拦截器
service.interceptors.request.use(
    config => {
        //  在请求发送前做的操作,如添加tokenz至header
        let token = getCookie('token');
        // 获取token并放入请求headers中
        if(token){
            config.headers['token'] = token
            config.headers.common['Authorization'] = 'Bearer ' + token;
        }
        return config
    },
    error => {
        console.log(error)
        return Promise.reject(error)
    }
)

// 响应拦截器
service.interceptors.response.use(
    response => {
        console.log(response)
        if(response.data&&response.data.code == 5001){
            Message.error(response.data.msg)
            let origin = window.location.origin;
            let paths = window.location.pathname.split("/");
            let host = origin+(paths.indexOf("alpha")>=0?"/alpha":"");
            window.location.href = host+"/login"
            return
        }

        if (response.data && response.data.code == 410) {
            Message.error('授权已到期，请联系管理员！');
            return
        }
        return response
    },
    error => {
        console.log(error)
        return Promise.reject(error)
    }
)

export default service;